package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.PublishTaskInterface;
import com.orhanobut.logger.Logger;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/6.
 */
public class PublishTaskPresenter extends BasePresenter<PublishTaskInterface> {
    private DataManager dataManager;
    private PublishTaskInterface mView;
    private Subscription mSubscription;
    public PublishTaskPresenter(DataManager dataManager){
        super();
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(PublishTaskInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void CommitData(Map<String,String> map){
        checkViewAttached();
        mSubscription = dataManager.CommitData(map).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<SuccessData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&& mView!=null){

                    mView.showError(e.toString());
                    Logger.e(e.toString()+"+++++++", "Error");
                }
            }

            @Override
            public void onNext(SuccessData s) {
                mView.ShowResult(s);
                Logger.e(s+"-------","success");
            }
        });


    }




}
